#include "core/vectors.hpp"
#include "yocto/ios/ocstream.hpp"
#include "yocto/code/unroll.hpp"

namespace FastMD
{
	Vector:: Vector( size_t n ) :
	numvtx_(n),
	vertex(memory::kind<MemoryKind>::acquire_as<V3D>(numvtx_) ),
	vertices(n)
	{
	}
	
	Vector:: ~Vector() throw() 
	{
		memory::kind<MemoryKind>::release_as<V3D>(vertex,numvtx_);
		(size_t&)vertices=0;
	}
	
	void Vector:: save_xyz( const string &filename, const string &particleID, const string &comment, bool append ) const
	{
		ios::ocstream fp( filename, append );
		fp("%u\n", unsigned(vertices) );
		fp.append(comment);fp.append("\n");
		for( size_t i=0; i < vertices; ++i)
		{
			const V3D &r = vertex[i];
			fp.append( particleID ); fp(" %g %g %g\n", r.x, r.y, r.z );
		}
	}
	
	void Vector:: ldz() throw()
	{
#define __LDZ(i) vertex[i].ldz()
		YOCTO_LOOP_FUNC_(vertices,__LDZ,0);
	}
}

